package com.community.web.master.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.common.utils.AliOssUtil;
import com.community.model.entity.Area;
import com.community.model.entity.Complaint;
import com.community.model.entity.GoodsCategory;
import com.community.model.entity.Store;
import com.community.web.master.constant.MessageConstant;
import com.community.web.master.domain.dto.AreaDto;
import com.community.web.master.domain.dto.PageAreaDto;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.exception.DataException;
import com.community.web.master.exception.DeletionNotAllowedException;
import com.community.web.master.mapper.AreaMapper;
import com.community.web.master.mapper.StoreMapper;
import com.community.web.master.service.IAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private StoreMapper storeMapper;

    @Override
    public void hello() {

    }

    /**
     * 分页查询
     * @param pageAreaDto
     * @return
     */

    @Override
    public PageResult pageQuery(PageAreaDto pageAreaDto) {
        //创建一个Page,泛型填对应entity
        IPage<Area> page = new Page<>(pageAreaDto.getPage(),pageAreaDto.getPageSize());
        //创建查询条件,更推荐使用LambdaQueryWrapper
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        //条件
        if(pageAreaDto.getAreaType() !=null){
            queryWrapper.eq("area_type",pageAreaDto.getAreaType());
        }
        if(pageAreaDto.getAreaName() !=null){
            queryWrapper.like("area_name",pageAreaDto.getAreaName());
        }
        areaMapper.selectPage(page,queryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }


    public void addArea(AreaDto areaDto) {
        //区域是否已经存在?
        QueryWrapper<Area> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("area_name",areaDto.getAreaName());
        Area one = areaMapper.selectOne(queryWrapper);
        if(one != null && one.getParentId()==areaDto.getParentId()){
            throw new DataException("区域已经存在");
        }
        //作校验,父id是否存在且合法
        Area parent = areaMapper.selectById(areaDto.getParentId());
        if (parent==null){
            throw new DataException(MessageConstant.DATA_FAILED);
        }
        if(parent.getAreaType()==3){
            throw new DataException("三级区域下不允许添加新区域");
        }

        Area area=new Area();
        //生成随机area_no
        UUID uuid = UUID.randomUUID();
        String randomUUIDString = uuid.toString().replace("-", "");

        BeanUtils.copyProperties(areaDto,area);
        area.setAreaNo(randomUUIDString);
        area.setAreaType(parent.getAreaType()+1);

        areaMapper.insert(area);
    }

    /**
     * 删除区域
     * @param id
     */
    @Override
    public void deleteArea(Integer id) {
        //判断是区域是否合法?(三级区域)
        Area area = areaMapper.selectById(id);
        if(area==null || area.getAreaType()!=3){
            throw new DataException(MessageConstant.DATA_FAILED);
        }
        //区域下如果有商店,无法删除,需要先删除商店
        QueryWrapper<Store> queryWrapper= new  QueryWrapper<Store>();
        queryWrapper.eq("area_id",area.getId());

        if (storeMapper.selectList(queryWrapper).size()>0){
            throw new DeletionNotAllowedException("有关联商店,无法删除");
        }
        //删除
        areaMapper.deleteById(id);
    }

    /**
     * 更新区域信息
     * @param areaDto
     */
    @Override
    public void updateArea(AreaDto areaDto) {

        Area data = new Area();
        //是否是同级区域?同级可以直接改
        Area area = areaMapper.selectById(areaDto.getId());
        Area newParent =new Area();
        if(area.getParentId()!=areaDto.getParentId()){
            newParent = areaMapper.selectById(areaDto.getParentId());
            if(newParent==null){
                //要改成一级目录,如果底下没有任何类别也可以改
                QueryWrapper<Area> wrapper = new QueryWrapper<Area>().eq("parent_id",areaDto.getId());
                if(areaMapper.exists(wrapper)){
                    throw new DataException("有子区域,无法更改区域类别");
                }
                areaMapper.updateById(data);
                return;
            }
            else if(newParent.getAreaType()==3){
                throw new DataException("三级区域下不允许添加新区域");
            }
        }

        if(area.getAreaType()==newParent.getAreaType()+1){
            BeanUtils.copyProperties(areaDto,data);
            areaMapper.updateById(data);
            return;
        }
        //不是同级,如果底下没有任何区域也可以改
        QueryWrapper<Area> wrapper = new QueryWrapper<Area>().eq("parent_id",areaDto.getId());
        if(areaMapper.exists(wrapper)){
            throw new DataException("有子区域,无法更改区域类别");
        }
        areaMapper.updateById(data);

    }
}
